package com.shixun.dds_11.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.shixun.dds_11.context.BaseContext;
import com.shixun.dds_11.entity.DdsCart;
import com.shixun.dds_11.entity.DdsGoods;
import com.shixun.dds_11.entity.DdsUser;
import com.shixun.dds_11.result.PageResult;
import com.shixun.dds_11.result.Result;
import com.shixun.dds_11.service.IDdsCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YSP
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/dds11/cart")
@Api(tags = "购物车接口")
@Slf4j
@CrossOrigin
public class DdsCartController {

    @Autowired
    IDdsCartService iDdsCartService;

    @PostMapping("/toCart")
    @ApiOperation("加入购物车")
    public Result insert(@RequestBody DdsGoods ddsGoods){

        log.info("加入购物车");

        return iDdsCartService.insert(ddsGoods);
    }

    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<PageResult> list(){
        log.info("查看购物车");
        Integer id = BaseContext.getCurrentId().intValue();

        return  Result.success(iDdsCartService.getList(id));
    }

    @PutMapping("/{id}/{count}")
    @ApiOperation("商品数量变化")
    public Result add(@PathVariable Integer id,@PathVariable Integer count){
        log.info("商品数量变化");
        return iDdsCartService.change(id,count);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除购物车")
    public Result drop(@PathVariable Integer id){
        log.info("删除购物车");
        iDdsCartService.removeById(id);
        return  Result.success();
    }

    @PostMapping("/toPay")
    @ApiOperation("去结算")
    public Result<Integer> toPay(@RequestBody ArrayList<Integer> ids){
        log.info("去结算");
        if (ids.size()==0|| ids==null)
            return Result.error("哥们别乱点");
        Integer id = BaseContext.getCurrentId().intValue();
        return  Result.success(iDdsCartService.toPay(ids,id));
    }

    @GetMapping()
    @ApiOperation("查询购物车数量")
    public Result num(){

        Integer id = BaseContext.getCurrentId().intValue();
        return Result.success(iDdsCartService.getByUserId(id));
    }


}